/*****************************************************
Copyright (C) 2019. All rights reserved.
File name     ：TestMain.c
Version       ：v1.0
Author        ：zhengqijun
Date          ：2020-04-26
Function List ：
Description   ：main function
******************************************************/

#include <stdio.h>  // for printf
#include "HuffmanTree.h"

#define MaxSize (50)

// PrintHuffmanTree for testing
static void PrintDepth(ElementType X, int depth);
static void PrintBinaryHeap(PriorityQueue H, int depth, int Index);
static void PrintHuffmanTree(HuffmanTree T, int depth);

void PrintDepth(ElementType X, int depth) {
    while (depth != 0) {
        printf("    ");
        --depth;
    }
    printf("%02d\n", X);
}

void PrintBinaryHeap(PriorityQueue H, int depth, int Index) {
    if (Index <= Size(H)) {
        PrintBinaryHeap(H, depth + 1, 2 * Index + 1);
        PrintDepth(Weight(Retrieve(H, Index)), depth);
        PrintBinaryHeap(H, depth + 1, 2 * Index);
    }
}

void PrintHuffmanTree(HuffmanTree T, int depth) {
    if (T == NULL) {
        printf("SearchTree is empty!\n");
        return;
    }

    if (Right(T) != NULL) {
        PrintHuffmanTree(Right(T), depth + 1);
    }

    PrintDepth(Weight(T), depth);

    if (Left(T) != NULL) {
        PrintHuffmanTree(Left(T), depth + 1);
    }
}

// main function
int main(void) {
    HuffmanTree T;
    PriorityQueue H;
    HeapElementType A[9];
    int i;

    printf("Insert priority queue: [ ");
    for (i = 0; i < 9; ++i) {
        A[i] = CreateNode(9 - i);
        printf("%d ", Weight(A[i]));
    }
    printf("]\n");

    H = BuildHeap(A, 9);

    printf("Print priority queue:\n");
    PrintBinaryHeap(H, 0, 1);
    printf("\n");

    T = Huffman(H);
    printf("Convert min heap to huffman tree ...\n");

    printf("Print huffman tree:\n");
    PrintHuffmanTree(T, 0);
    printf("\n");

    T = MakeEmptyTree(T);

    return 0;
}

// EOF

